﻿@inherits RazorFunction
@using System.Web.Caching
@using Composite.Core.WebClient.Renderings.Page
@functions {
    public override string FunctionDescription
    {
        get { return "Adds the Facebook Like Box to a web page."; }
    }

    [FunctionParameter(Label = "Facebook page URL", Help = "The URL of the Facebook page for this Like box. For example, http://www.facebook.com/CompositeC1")]
    public string URL { get; set; }

    [FunctionParameter(Label = "Color Scheme", Help = "The color scheme for the plugin. Options: 'light', 'dark'", DefaultValue = "light", 
                     WidgetMarkup = @"<f:widgetfunction xmlns:f=""http://www.composite.net/ns/function/1.0"" name=""Composite.Widgets.String.Selector""><f:param name=""Options""><f:function name=""Composite.Utils.String.Split""><f:param name=""String"" value=""light,dark"" /></f:function></f:param></f:widgetfunction>")]
    public string ColorScheme { get; set; }


    [FunctionParameter(Label = "Options", Help = "Options to customize the Like Box's appearance.", DefaultValue = "Show Friends’ Faces,Show Posts,Show Header,Show Border",
                     WidgetMarkup = @"<f:widgetfunction xmlns:f=""http://www.composite.net/ns/function/1.0"" name=""Composite.Widgets.String.Selector""><f:param name=""Options""><f:function name=""Composite.Utils.String.Split""><f:param name=""String"" value=""Show Friends’ Faces,Show Posts,Show Header,Show Border"" /></f:function></f:param><f:param name=""Multiple"" value=""True"" /></f:widgetfunction>")]
    public string Options { get; set; }
    

    private XDocument GetXml(string url)
    {

        if (HttpContext.Current.Cache[url] == null)
        {
            HttpContext.Current.Cache.Add(url, XDocument.Load(url),
                             null, DateTime.Now.AddSeconds(3600),
                             Cache.NoSlidingExpiration,
                             CacheItemPriority.Default,
                             null);
        }
        return HttpContext.Current.Cache[url] as XDocument;
    }
}
@{
    if (PageRenderer.RenderingReason == RenderingReason.ScreenshotGeneration)
    {
        return;
    }
    var cultures = GetXml("http://www.facebook.com/translations/FacebookLocales.xml");
    var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture.Name.Replace("-", "_");
    currentCulture = cultures.Descendants("representation").Any(c => c.Value == currentCulture) ? currentCulture : "en_US";
    var showFaces = Options.Contains("Show Friends’ Faces").ToString().ToLower();
    var showStream = Options.Contains("Show Posts").ToString().ToLower();
    var showHeader = Options.Contains("Show Header").ToString().ToLower();
    var forceWall = Options.Contains("Show Posts").ToString().ToLower();
    var showBorder = Options.Contains("Show Border").ToString().ToLower();
}

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script id="facebook-all-js" src="http://connect.facebook.net/@currentCulture/all.js#xfbml=1"></script>
    <style id="facebook-likebox" type="text/css">
       .fb_iframe_widget, .fb_iframe_widget span, .fb_iframe_widget span iframe[style] {
            width: 100% !important;
        }
    </style>
</head>
<body>
    <div id="fb-root">
        <fb:like-box xmlns:fb="http://www.facebook.com/plugins/" href="@URL"
                     show_faces="@showFaces"
                     show_border="@showBorder"
                     stream="@showStream"
                     header="@showHeader"
                     colorscheme="@ColorScheme"
                     force_wall="@forceWall">
        </fb:like-box>
    </div>
</body>
</html>